﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_artcles_class_Default : System.Web.UI.Page
{ 
    protected void Page_Load(object sender, EventArgs e)
    {
        scriptmsg.Text = "";
        if (!IsPostBack)
        {
            LoadClassList();
        }

    }

    protected void LoadClassList()
    {
        DataSet ds_class = new DataSet();
        ds_class = new Cstudy.BLL.postClass().SelectPostClassOrderBy();
        ClassListBox.DataSource = ds_class.Tables[0];
        ClassListBox.DataBind();
    }

    protected void ClassListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        NameBox.Text = ClassListBox.SelectedRow.Cells[2].Text;
        RemarkBox.Text = ClassListBox.SelectedRow.Cells[4].Text;
        OrderBox.Text = ClassListBox.SelectedRow.Cells[3].Text;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ClassListBox.SelectedIndex < 0)
        {
            scriptmsg.Text = "<script>alert('未选择修改项!')</script>";
        }
        else if (NameBox.Text.Trim() == "" && OrderBox.Text.Trim() == "")
        {
            scriptmsg.Text = "<script>alert('数据填写不完整!')</script>";
        }
        else
        {
            Cstudy.MODEL.postclass postclass = new Cstudy.MODEL.postclass();
            postclass.CID = int.Parse(ClassListBox.SelectedRow.Cells[1].Text);
            postclass.CName = NameBox.Text;
            postclass.COrder = int.Parse(OrderBox.Text);
            postclass.CRemarks = RemarkBox.Text;
            if (new Cstudy.BLL.postClass().UpdatePostClass(postclass))
            {
                scriptmsg.Text = "<script>alert('修改成功!');window.location.href='Default.aspx';</script>";
            }
            else
            {
                scriptmsg.Text = "<script>alert('修改失败!')</script>";
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (NameBox.Text.Trim() == "" && OrderBox.Text.Trim() == "")
        {
            scriptmsg.Text = "<script>alert('数据填写不完整!')</script>";
        }
        else
        {
            Cstudy.MODEL.postclass postclass = new Cstudy.MODEL.postclass();
            postclass.CName = NameBox.Text;
            postclass.COrder = int.Parse(OrderBox.Text);
            postclass.CRemarks = RemarkBox.Text;
            if (new Cstudy.BLL.postClass().AddPostClass(postclass))
            {
                scriptmsg.Text = "<script>alert('添加成功!')</script>";
                Response.Redirect("Default.aspx");
            }
            else
            {
                scriptmsg.Text = "<script>alert('添加失败!')</script>";
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        try
        {
            int id = int.Parse(ClassListBox.SelectedRow.Cells[1].Text);
            if (new Cstudy.BLL.postClass().DeletePostClass(id))
            {
                scriptmsg.Text = "<script>alert('删除成功!')</script>";
                Response.Redirect("Default.aspx");
            }
            else
            {
                scriptmsg.Text = "<script>alert('删除失败!')</script>";
            }
        }
        catch (NullReferenceException ex)
        {
            scriptmsg.Text = "<script>alert('删除失败!没有选择删除项！')</script>";
        }
    }
}